Each letter in the alphabet is numbered from 0 to 25 (i.e. 'a' -> 0, 'b' -> 1, 'c' -> 2, ... , 'z' -> 25).
In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.
Return true if s is a well-spaced string, otherwise return false.
檢查兩個一樣的字母,距離有沒有跟他所給的distance一樣
from collections import defaultdict
class Solution:
#s裡面的每個字母都必定出現兩次
#distance裡面儲存了每個字母所必須相隔的距離
#對了,其實可以用ord處理就好
def checkDistances(self, s: str, distance: List[int]) -> bool:
indexDict = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7,'i':8,'j':9,
'k':10,'l':11,'m':12,'n':13,'o':14,'p':15,'q':16,'r':17,'s':18,
't':19,'u':20,'v':21,'w':22,'x':23,'y':24,'z':25}
rec = defaultdict(list)
for i in range(len(s)):
rec[s[i]].append(i)
for i,v in rec.items():
if distance[indexDict[i]] != v[1]-v[0]-1:
return 0
return 1
class Solution:
#strictly increasing
#不會有重複的
def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]:
return sorted(set(arr1) & set(arr2) & set(arr3))
Given a string n that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n.
用1010101所組成的數字組成n
class Solution:
#由0跟1組成的數字組成n
def minPartitions(self, n: str) -> int:
nSet = set(n)
return max(nSet)
Return the sorted string. If there are multiple answers, return any of them.
class Solution:
#依照出現的次數排序(從出現次數多到少)
def frequencySort(self, s: str) -> str:
sC = Counter(s)
numList = sorted(sC.items(), key = lambda x:-x[1])
ans = "".join([i[0]*i[1] for i in numList])
return ans
以上就是今天了練習感謝大家QQ